package com.server.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import com.server.background.data.RankData;

public class UpdateData {
	public static UpdateData instance;
	public static UpdateData GetInstance(){
		if(instance == null){
			instance = new UpdateData();
		}
		return instance;
	}
	
	/**
	 * 修改管理员密码
	 * @param uname
	 * @param password
	 * @return
	 * @throws SQLException
	 */
	public boolean ChangeAccountPassword(String uname, String password) throws SQLException {
		boolean isSuccess = false;
		String sql = "update account set password = ? where uname = ?";
		Connection conn = DBPool.getConnection();
    	PreparedStatement pstmt = null;
    	try {
    		pstmt = conn.prepareStatement(sql);
        	pstmt.setString(1, password);	
        	pstmt.setString(2, uname);	
    	    pstmt.executeUpdate();
    	    isSuccess = true;
    	}catch (SQLException e) {
			e.printStackTrace();
		}
    	pstmt.close();
    	conn.commit();
    	conn.close();
		return isSuccess;
	}
	
	/**
	 * 修改管理员应用权限
	 * @param uname
	 * @param password
	 * @return
	 * @throws SQLException
	 */
	public boolean ChangeAccountPower(String uname, String data) throws SQLException {
		boolean isSuccess = false;
		String sql = "update account set app_power = ? where uname = ?";
		Connection conn = DBPool.getConnection();
    	PreparedStatement pstmt = null;
    	try {
    		pstmt = conn.prepareStatement(sql);
        	pstmt.setString(1, data);	
        	pstmt.setString(2, uname);	
    	    pstmt.executeUpdate();
    	    isSuccess = true;
    	}catch (SQLException e) {
			e.printStackTrace();
		}
    	pstmt.close();
    	conn.commit();
    	conn.close();
		return isSuccess;
	}
	
	/**
	 * 修改应用的微信id
	 * @param wx_appid
	 * @param appid
	 * @return
	 * @throws SQLException
	 */
	public boolean ChangeAppWxId(String wx_appid, Long appid) throws SQLException {
		boolean isSuccess = false;
		String sql = "update app set wx_appid = ? where id = ?";
		Connection conn = DBPool.getConnection();
    	PreparedStatement pstmt = null;
    	try {
    		pstmt = conn.prepareStatement(sql);
        	pstmt.setString(1, wx_appid);	
        	pstmt.setLong(2, appid);	
    	    pstmt.executeUpdate();
    	    isSuccess = true;
    	}catch (SQLException e) {
			e.printStackTrace();
		}
    	pstmt.close();
    	conn.commit();
    	conn.close();
		return isSuccess;
	}
	
	/**
	 * 修改应用的微信secret
	 * @param wx_appid
	 * @param appid
	 * @return
	 * @throws SQLException
	 */
	public boolean ChangeAppWxSecret(String wx_secret, Long appid) throws SQLException {
		boolean isSuccess = false;
		String sql = "update app set wx_secret = ? where id = ?";
		Connection conn = DBPool.getConnection();
    	PreparedStatement pstmt = null;
    	try {
    		pstmt = conn.prepareStatement(sql);
        	pstmt.setString(1, wx_secret);	
        	pstmt.setLong(2, appid);	
    	    pstmt.executeUpdate();
    	    isSuccess = true;
    	}catch (SQLException e) {
			e.printStackTrace();
		}
    	pstmt.close();
    	conn.commit();
    	conn.close();
		return isSuccess;
	}
	
	/**
	 * 更新开关
	 */
	public boolean UpdateSwitch(long appid, byte state, String gameswitch) throws SQLException{
		boolean isSuccess = false;
		String sql = "update app_switch set " + gameswitch + " = ? where id = ?";
		Connection conn = DBPool.getConnection();
    	PreparedStatement pstmt = null;
    	try {
    		pstmt = conn.prepareStatement(sql);
    		pstmt.setByte(1, state);
    		pstmt.setLong(2, appid);
	        pstmt.executeUpdate();
	        isSuccess = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
    	pstmt.close();
    	conn.commit();
    	conn.close();
    	return isSuccess;
	}
	
	/**
	 * 更新存档数据
	 */
	public int UpdateGameData(long uid ,long appid, String key,String value) throws SQLException{
		int state = 0;
		String sql = "update data set value = ? where appid=? and datakey=? and uid=?";
		Connection conn = DBPool.getConnection();
    	PreparedStatement pstmt = null;
    	try {
    		pstmt = conn.prepareStatement(sql);
    		pstmt.setString(1, value);
    		pstmt.setLong(2, appid);
    		pstmt.setString(3, key);
    		pstmt.setLong(4, uid);
	        pstmt.executeUpdate();
	        state = 1;
		} catch (SQLException e) {
			e.printStackTrace();
			state = -3;
		}
    	pstmt.close();
    	conn.commit();
    	conn.close();
    	return state;
	}
	
	/**
	 * 更新应用注册人数
	 */
	public int UpdateAppRegisterCount(long appid) throws SQLException{
		int state = 0;
		String sql = "update app set register_count = register_count + 1 , game_count = game_count + 1 where id=?";
		Connection conn = DBPool.getConnection();
    	PreparedStatement pstmt = null;
    	try {
    		pstmt = conn.prepareStatement(sql);
    		pstmt.setLong(1, appid);
    		state = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			state = -3;
		}
    	pstmt.close();
    	conn.commit();
    	conn.close();
    	return state;
	}
	
	/**
	 * 更新应用游戏人数
	 */
	public int UpdateAppGameCount(long appid) throws SQLException{
		int state = 0;
		String sql = "update app set game_count = game_count + 1 where id=?";
		Connection conn = DBPool.getConnection();
    	PreparedStatement pstmt = null;
    	try {
    		pstmt = conn.prepareStatement(sql);
    		pstmt.setLong(1, appid);
    		state = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			state = -3;
		}
    	pstmt.close();
    	conn.commit();
    	conn.close();
    	return state;
	}
	
	/**
	 * 上传排行榜数据
	 * @param rkname
	 * @param uid
	 * @param uname
	 * @param uhead
	 * @param value
	 * @param info
	 * @return
	 * @throws SQLException
	 */
	public int PutRankData(String rkname,long uid,String uname,String uhead,int value, String info) throws SQLException {
		int state = 0;
		String sql = "insert into " + rkname + " (uid,uname,uhead,value,info) values(?,?,?,?,?) ON DUPLICATE KEY UPDATE uname=?,uhead=?,value=?,info=?";
		Connection conn = DBPool.getConnection();
    	PreparedStatement pstmt = null;
    	try {
    		pstmt = conn.prepareStatement(sql);
    		pstmt.setLong(1, uid);
    		pstmt.setString(2, uname);
    		pstmt.setString(3, uhead);
    		pstmt.setInt(4, value);
    		pstmt.setString(5, info);
    		pstmt.setString(6, uname);
    		pstmt.setString(7, uhead);
    		pstmt.setInt(8, value);
    		pstmt.setString(9, info);
    		state = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			state = -3;
		}
    	pstmt.close();
    	conn.commit();
    	conn.close();
    	return state;
	}
	
	/**
	 * 无头像上传
	 * @param rkname
	 * @param uid
	 * @param uname
	 * @param value
	 * @param info
	 * @return
	 * @throws SQLException
	 */
	public int PutRankData(String rkname,long uid,String uname,int value, String info) throws SQLException {
		int state = 0;
		String sql = "insert into " + rkname + " (uid,uname,value,info) values(?,?,?,?) ON DUPLICATE KEY UPDATE uname=?,value=?,info=?";
		Connection conn = DBPool.getConnection();
    	PreparedStatement pstmt = null;
    	try {
    		pstmt = conn.prepareStatement(sql);
    		pstmt.setLong(1, uid);
    		pstmt.setString(2, uname);
    		pstmt.setInt(3, value);
    		pstmt.setString(4, info);
    		pstmt.setString(5, uname);
    		pstmt.setInt(6, value);
    		pstmt.setString(7, info);
    		state = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			state = -3;
		}
    	pstmt.close();
    	conn.commit();
    	conn.close();
    	return state;
	}
	
	/**
	 * 保存排行榜列表
	 */
	public void SaveRankList(String tb, List<RankData> r_list) {
		if(r_list == null || r_list.size() == 0)return;
		String sql = "TRUNCATE TABLE " + tb;
		Connection conn = DBPool.getConnection();
    	PreparedStatement pstmt = null;
    	
    	try{
    		pstmt = conn.prepareStatement(sql);
    		pstmt.executeUpdate();
    		pstmt.close();
    		sql = "INSERT IGNORE INTO " + tb + " (uid,uname,uhead,value,info) value(?,?,?,?,?)";
    		pstmt = conn.prepareStatement(sql);
    		for(RankData rd : r_list) {
    			pstmt.setLong(1, rd.uid);
    			pstmt.setString(2, rd.name);
    			pstmt.setString(3, rd.head);
    			pstmt.setLong(4, rd.value);
    			pstmt.setString(5, rd.info);
    			pstmt.addBatch();
    		}
    		pstmt.executeBatch();
    		//提交修改
    		conn.commit();
    	}catch(Exception e){
        	e.printStackTrace();
        }finally {
        	try {
        		if(pstmt != null) {
            		pstmt.close();
    				pstmt = null;
    			}
    	    	conn.commit();
    	    	conn.close();
    	    	conn = null;
        	} catch (SQLException e) {
        		e.printStackTrace();
        	}
        }
	}
}
